package cn.handong.tool.Java文件操作.txt;

//import org.apache.log4j.Logger;
import org.junit.Test;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 日志学习
 * https://blog.csdn.net/imjcoder/article/details/121688831
 */

public class SelectHouse {
//    private static Logger logger = Logger.getLogger(SelectHouse.class.toString());

    /**
     * 逐行读取TXT
     */
    @Test
    public void readTxtToObject() {
        List<House> houses = new ArrayList<>();
        try {
            //绝对路径
//            String path = "C:\\Users\\sqkb8v\\Documents\\list.txt";
            String path = "list.txt"; //使用相对路径的话要放在和src同基本的文件夹
            BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(path)), StandardCharsets.UTF_8));
            String lineTxt;
            // 逐行读取
            while ((lineTxt = br.readLine()) != null) {
                houses.add(new House(lineTxt));
//                logger.info(lineTxt);
            }
            br.close();
        } catch (Exception e) {
//            logger.error("Error Message :"+ e);
        }
        Collections.sort(houses);
        Integer budget = 4411817;
        List<House> collect = houses.stream().filter(item -> item.getPrice() <= budget && item.getFloorNum() <= 13).collect(Collectors.toList());
        System.out.println(collect);
//        for (House house:houses){
//            if (house.getPrice() > budget){
//                continue;
//            }
//            System.out.println(house.getName());
//        }
//        logger.info(houses);
    }


    /**
     * 写入txt
     */
    @Test
    public void testWriteToTxt() {
        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        messageList.add("ok,0");
        messageList.add("no,1");
        messageList.add("pass,2");
        messageList.add("okay,3");

        try {
            writeToTxt(folder, file, messageList);
        } catch (FileNotFoundException e) {
//            logger.error("Error Message :", e);
        }

    }

    /**
     * 读取分析后写入
     */
    @Test
    public void readAndWrite(){
        List<House> houses = new ArrayList<>();
        /**
         * private Integer roomNum;
         *     private Double unitPrice;
         *     private Integer price;
         */
        try {
            //绝对路径
//            String path = "C:\\Users\\sqkb8v\\Documents\\list.txt";
            String path = "000.txt"; //使用相对路径的话要放在和src同基本的文件夹
            BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(path)), StandardCharsets.UTF_8));
            String lineTxt;
            String fileName = "000.txt";   //文件名   楼号.txt
            Integer flag = 0;  //1:室号   2:单价   3:总价   4:楼号   5:建筑面积
            Integer num = 0; //第几个数据
            // 逐行读取
//            while ((lineTxt = br.readLine()) != null) {
//                if (lineTxt == "门牌号"){
//                    flag = 4;
//                }else if (lineTxt == "室号"){
//                    flag = 1;
//                }else if (lineTxt == "单价"){
//                    flag = 2;
//                }else if (lineTxt == "总价"){
//                    flag = 3;
//                }else if (lineTxt == "建筑面积"){
//                    flag = 5;
//                }
//
//                if (flag == 1){
//                    if (houses.size() >= num){
//                        House house = new House();
//                        house.setRoomNum(Integer.valueOf(lineTxt));
//                        houses.add(house);
//                        num++;
//                    }else {
//                        houses.get(num).setRoomNum(Integer.valueOf(lineTxt));
//                        num++;
//                    }
//                }else if (flag == 2){
//                    if (houses.size() >= num){
//                        House house = new House();
//                        house.setUnitPrice(lineTxt);
//                        houses.add(house);
//                        num++;
//                    }else {
//                        houses.get(num).setUnitPrice(lineTxt);
//                        num++;
//                    }
//                }



//                houses.add(new House(lineTxt));
//                logger.info(lineTxt);
//            }
            br.close();
        } catch (Exception e) {
//            logger.error("Error Message :"+ e);
        }



        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        messageList.add("ok,0");
        messageList.add("no,1");
        messageList.add("pass,2");
        messageList.add("okay,3");

        try {
            writeToTxt(folder, file, messageList);
        } catch (FileNotFoundException e) {
//            logger.error("Error Message :", e);
        }

    }


    /**
     * 方法1写入txt
     *
//     * @param filePath
     * @param message
     * @throws IOException
     */
    public void writetoTxt1(String folder, String filename, String message) throws IOException {
        File files = new File(folder);
        files.mkdirs();
        File file = new File(folder + File.separator + filename);
        file.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(message);
        out.flush();

    }

    /**
     * 方法2写入txt
     *
//     * @param foler
     * @param file
     * @param messageList
     * @throws FileNotFoundException
     */
    public void writeToTxt(String folder, String file, List<String> messageList) throws FileNotFoundException {
        File files = new File(folder);
        files.mkdirs();

        FileOutputStream fos = new FileOutputStream(folder + File.separator + file);

        // 逐行写入
        PrintWriter pw = new PrintWriter(fos);
        for (String message : messageList) {
            pw.println(message);
        }
        pw.close();

    }

    /**
     * 测试方法2写入txt
     */
    @Test
    public void writeToTxt() {

//        String folder = "C:\\Test0";
        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String filename = "000.txt";

        String message = "this is message \n";
        for (int i = 0; i < 3; i++) {
            message += "abc \n";
        }
        try {
            writetoTxt1(folder, filename, message);
        } catch (IOException e) {
//            logger.error("Error Message :", e);
        }

    }



}